package controller;
import	java.util.List;


import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.*;
import mapper.PermissionMapper;
import mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.RoleService;

import java.util.List;

/**
 * @Author Administrator
 * @create 2020.06.18 上午 8:45
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
//    @LogData(logType = LogType.LOGIN,description = "角色信息查看")
        @ResponseBody
        @GetMapping("/list")
        public MyResult<List<Role>> layList(LayRequest layRequest){
            PageInfo<Role> rolePageInfo=roleService.selectByMap(layRequest);
            return new MyResult<>(0,rolePageInfo.getTotal(),rolePageInfo.getList());
        }
    @LogData(logType = LogType.INSERT,description = "角色添加")
    @ResponseBody
        @RequestMapping("/add")
    public MyResult insert(Role role){
            boolean flag = roleService.insert(role);
            // 重定向 "redirect:重定向的请求地址"; 通常重定向到Controller方法
            return new MyResult<>(0);//重定向到list请求
        }

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap){
        //查询角色信息
        Role role=roleService.selectById(id);
        //将角色信息传递到页面
        modelMap.addAttribute("role",role);
        return "update.jsp";
    }
//    @LogData(logType = LogType.UPDATE,description = "角色信息修改")
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(Role role){
        boolean flag= roleService.update(role);
        return new MyResult<>(0);
    }
    @LogData(logType = LogType.DELETE,description = "角色删除")
    @RequestMapping("/delete")
    @ResponseBody
    public MyResult delete(Integer id){
        boolean flag= roleService.delete(id);
        return new MyResult<>(0);
    }
    @Autowired
    private PermissionMapper permissionMapper;

    @GetMapping("/toPermission")
    public String toPermission(Integer id,ModelMap modelMap){
        modelMap.addAttribute("id",id);
        return "permission.jsp";
    }
    @LogData(logType = LogType.LOGIN,description = "角色权限查看")
        @ResponseBody
        @GetMapping("/permission")
    public MyResult<List<TreeVo>> permission(Integer id){
        List<TreeVo> treeVos = permissionMapper.selectAllPermissionToTree(-1);
//            System.out.println(treeVos);
            System.out.println();
            Role role = roleService.selectRoleAndPer(id);

            for (TreeVo t:treeVos){
                for (TreeVo t2:t.getChildren()){
                    for (Permission r1:role.getPermissionList()){
                        if (r1.getTitle().equals(t2.getTitle())){
                            t2.setChecked(true);
                        }
                    }
                }
                for (Permission r2:role.getPermissionList()){
                    if (r2.getTitle().equals(t.getTitle())){
                        t.setChecked(true);
                    }
                }
            }

            System.out.println(role+"===============");
        return new MyResult<>(0,treeVos);
    }
    @LogData(logType = LogType.LOGIN,description = "查看所有权限")
    @ResponseBody
    @GetMapping("/permissions")
    public MyResult<List<TreeVo>> permissions(){
        List<TreeVo> treeVos = permissionMapper.selectAllPermissionToTree(-1);

        return new MyResult<>(0,treeVos);
    }
}
